// pages/finance/index/index.js
var http = require('../../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      page: 1,
      size: 7,
      state: 0
    },
    moneyObj: {
      userMoney: 0, //可用余额
      billMoney: 0 //待入帐金额
    },
    userCardList: []
  },
  
  //点击余额提现
  withdraw() {
    if (this.data.userCardList.length===0){
      wx.showModal({
        title: '提示',
        content: '使用提现功能需添加一张支持提现的储蓄卡',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../withdraw/step1/step1',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      wx.navigateTo({
        url: '../withdraw/step2/step2',
      })
    }
    
  },
  //根据类型不同跳到不同页面
  toPage(e) {
    let index = parseInt(e.currentTarget.dataset.type);
    if (index === 1) {
      wx.navigateTo({
        url: '../../../pages/finance/bills/index/index'
      })
    } else if (index === 2) {
      wx.navigateTo({
        url: '../../../pages/finance/orders/index/index'
      })
    } else {
      wx.navigateTo({
        url: '../../../pages/finance/withdraw/index/index'
      })
    }
  },
  //获取可用余额
  getUserMoney() {
    let that = this;
    http.GET({
      url: 'api/order/withdrawLogList',
      data: that.data.form,
      success: function(res) {
        that.setData({
          'moneyObj.userMoney': Number(res.data.data.user_money)
        });
        that.setData({
          'moneyObj.billMoney': Number(res.data.data.bill_amount)
        });
        that.setData({
          'moneyObj.allMoney': (Number(res.data.data.bill_amount) + Number(res.data.data.user_money)).toFixed(2)
        });
        wx.setStorage({
          key: 'moneyObj',
          data: that.data.moneyObj
        })
      }
    })
  },
  //获取待入赃账单
  getBillList() {
    let that = this;
    http.GET({
      url: 'api/order/billList',
      data: that.data.form,
      success: function(res) {
        if (res.data.error === 0) {
          let list = res.data.data.data;
          let billMoney = 0;
          for (let index = 0; index < list.length; index++) {
            billMoney = billMoney + Number(list[index].bill_account_money);
          }
        }
      }
    })
  },
  //获取银行卡信息接口
  getUserCardList() {
    let that = this;
    http.GET({
      url: 'api/user/userCardInfo',
      success: function(res) {
        if (res.data.error === 0) {
          let list = res.data.data;
          that.setData({
            "userCardList": list
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getUserMoney();
    this.getUserCardList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  
})